#include <bits/stdc++.h>
using namespace std;
const int maxn=2e3;
int n,m,v[maxn];
double ma;
string t,s[maxn],ans;
int read(){
    int s=0;
    char ch=getchar(),last=' ';
    while(ch<'0'||ch>'9')
        last=ch,ch=getchar();
    while(ch>='0'&&ch<='9')
        s=(s<<1)+(s<<3)+(ch^48),ch=getchar();
    return last=='-'?-s:s;
}
void solve(int x){
    if(x==n){
        int k=0,res=1;
        for(int i=1;i<=m;i++)
            for(int j=0;j<n;j++)
                for(int l=0;s[i][l];l++){
                    if(t[j+l]!=s[i][l])
                        break;
                    if(l==s[i].size()-1)
                        k++,res*=v[i];
                }
        double p=pow(res,1/(k*1.0));
        if(p>ma)
            ma=p,ans=t;
        return;
    }
    if(t[x]!='.'){
        solve(x+1);
        return;
    }
    for(int i=0;i<=9;i++){
        t[x]=i+'0';
        solve(x+1);
    }
}
signed main(){
    freopen("magic.in","r",stdin);
    freopen("magic.out","w",stdout);
    n=read(),m=read();
    cin>>t;
    for(int i=1;i<=m;i++)
        cin>>s[i],v[i]=read();
    solve(0);
    cout<<ans<<endl;
    return 0;
}